package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.user;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name="loginBean")
@ViewScoped
public class LoginBean {
	
	@ManagedProperty(value="#{usuarioBean}")
	private UsuarioBean userBean;
	
	@ManagedProperty(value = "#{msgs['login.error']}")
	private String errorMessage;
	
	private String username;
	
	private String password;
	
	/**
	 * @return the userBean
	 */
	public UsuarioBean getUserBean() {
		return userBean;
	}
	/**
	 * @param userBean the userBean to set
	 */
	public void setUserBean(UsuarioBean userBean) {
		this.userBean = userBean;
	}
	/**
	 * @return the userName
	 */
	public String getUsername() {
		return username;
	}
	/**
	 * @return the errorMessage
	 */
	public String getErrorMessage() {
		return errorMessage;
	}
	/**
	 * @param errorMessage the errorMessage to set
	 */
	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
	/**
	 * @param userName the userName to set
	 */
	public void setUsername(String userName) {
		this.username = userName;
	}
	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	
	public void logIn(){
		userBean.logIn(this.username, this.password);
		
	}

	public String enter(){
		if(! userBean.isLogged()){
			FacesContext.getCurrentInstance().addMessage( null,
				new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage,	errorMessage));
			return null;
		}
		
		if(userBean.isSimpleUser())
			return "simpleuser";
		else if (userBean.isAdmin())
			return "adminuser";
		
		return null;
	}
}
